package cn.iocoder.yudao.module.pay.transfer.api;

import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.module.api.pay.transfer.PayTransferApi;
import cn.iocoder.yudao.module.pay.bank.dal.dataobject.PayBankDO;
import cn.iocoder.yudao.module.pay.bank.service.PayBankService;
import cn.iocoder.yudao.module.pay.framework.impl.mallbook.MallBookPayClientConfig;
import cn.iocoder.yudao.module.pay.splitting.pojo.po.SplittingMerchant;
import cn.iocoder.yudao.module.pay.splitting.service.core.CoreSplittingMerchantService;
import com.trhui.mallbook.client.UserAccountClient;
import com.trhui.mallbook.domain.common.BaseResponse;
import com.trhui.mallbook.domain.request.hf.HfBindAccountRequest;
import com.trhui.mallbook.domain.request.hf.HfUnbindAccountRequest;
import com.trhui.mallbook.domain.response.UnbindAccountResponse;
import com.trhui.mallbook.domain.response.hf.HfBindAccountResponse;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

/**
 * 转账单 API 实现类
 *
 * @author jason
 * @author 山野羡民
 */
@Slf4j
@Service
@Validated
public class PayTransferApiImpl implements PayTransferApi {

    @Resource
    private CoreSplittingMerchantService coreSplittingMerchantService;
    @Resource
    private PayBankService payBankService;

    @Override
    public void bindBankAccount(Long userId) {
        PayBankDO bank = payBankService.getDefaultBank(userId);
        SplittingMerchant merchant = validateSplittingMerchant(bank);
        HfBindAccountRequest request = new HfBindAccountRequest();
        /*
         * 子商户编号
         */
        request.setUserId(merchant.getChannelUserId());
        /*
         * 身份证号
         */
        request.setCertId(merchant.getEidNo());
        /*
         * 结算账户名
         */
        request.setCardName(merchant.getName());
        /*
         * 结算账户号
         */
        request.setCardNo(bank.getCardNo());
        /*
         * 银行账户类型 1-对公；2-对私
         */
        request.setBankAcctType("2");
        /*
         * 手机号
         */
        request.setPhone(merchant.getPhone());
        /*
         * 开户银行名称
         */
        request.setBankName(bank.getCardName());
        /*
         * 银行编码，银行账户类型为对公时必填，链接地址：http://www.Mallbook.cn/file/hf/list.xlsx
         */
        //request.setBankCode("03060000");
        /*
         * 省份编码，银行账户类型为对公时必填
         */
        //request.setProvCode("0035");
        /*
         * 地区编码，银行账户类型为对公时必填
         */
        //request.setAreaCode("3501");
        request.setMerchantNo(MallBookPayClientConfig.Holder.INSTANCE.getMerchantNo());
        request.setMerOrderId(MallBookPayClientConfig.Holder.INSTANCE.generateRequestNo());
        BaseResponse<HfBindAccountResponse> response = UserAccountClient.hfBindAccount(request);
        log.info("[bindBankAccount] UserAccountClient.hfBindAccount: code={}, msg={}", response.getCode(), response.getMsg());
        Assertions.isTrue(response.success(), response.getMsg());
        HfBindAccountResponse data = response.getData();
        Assertions.isTrue("1".equals(data.getStatus()), data.getErrMsg());
    }

    @Override
    public void unbindBankAccount(Long userId) {
        PayBankDO bank = payBankService.getDefaultBank(userId);
        SplittingMerchant merchant = validateSplittingMerchant(bank);
        HfUnbindAccountRequest request = new HfUnbindAccountRequest();
        request.setMerOrderId(MallBookPayClientConfig.Holder.INSTANCE.generateRequestNo());
        request.setMerchantNo(MallBookPayClientConfig.Holder.INSTANCE.getMerchantNo());
        request.setUserId(merchant.getChannelUserId());
        BaseResponse<UnbindAccountResponse> response = UserAccountClient.hfUnbindAccount(request);
        Assertions.isTrue(response.success(), response.getMsg());
        UnbindAccountResponse data = response.getData();
        Assertions.isTrue("1".equals(data.getStatus()), data.getReason());
    }

    @Nonnull
    private SplittingMerchant validateSplittingMerchant(PayBankDO bank) {
        Assertions.notNull(bank, "银行卡不存在");
        Assertions.notBlank(bank.getCardMobile(), "未提供银行卡预留手机号码");
        SplittingMerchant merchant = coreSplittingMerchantService.getOrCreate(bank.getCardMobile());
        Assertions.notNull(merchant, "分账商户号不存在：" + bank.getCardMobile());
        Assertions.notBlank(merchant.getChannelUserId(), "第三方分账商户号未注册成功，请重试");
        return merchant;
    }


}
